﻿using AnesSystem.Models;
using AnesSystem.Models.Model;
using AnesSystem.Services;
using AnesSystem.Services.Configuration;
using AnesSystem.ViewModels.LaborAnalgesia;
using Common;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    public class LaborTemplateCtrlViewModel : BaseViewModel<LaborTemplateCtrlViewModel>
    {
        #region Variables
        private ObservableCollection<LaborTemplateModel> _displayTemplateList;
        private List<LaborTemplateModel> _templateList;
        private LaborTemplateModel _selectRecordTemplate;
        private List<LaborTemplateModel> _tempLateList2;
        private string _searchStr;



        #endregion


        #region Constructor
        public LaborTemplateCtrlViewModel()
        {
            _displayTemplateList = new ObservableCollection<LaborTemplateModel>();
            _templateList = new List<LaborTemplateModel>();
            SelectRecordTemplate = new LaborTemplateModel();
            InitData();
        }


        #endregion


        #region Properties
        public ObservableCollection<LaborTemplateModel> DisplayTemplateList
        {
            get
            {
                return _displayTemplateList;
            }

            set
            {
                _displayTemplateList = value;
            }
        }

        public List<LaborTemplateModel> TemplateList
        {
            get
            {
                return _templateList;
            }

            set
            {
                _templateList = value;
            }
        }

        public LaborTemplateModel SelectRecordTemplate
        {
            get
            {
                return _selectRecordTemplate;
            }

            set
            {
                _selectRecordTemplate = value;
            }
        }

        public List<LaborTemplateModel> TempLateList2
        {
            get
            {
                return _tempLateList2;
            }

            set
            {
                _tempLateList2 = value;
            }
        }

        public string SearchStr
        {
            get
            {
                if (_searchStr == null)
                {
                    return "";
                }
                return _searchStr;
            }

            set
            {
                _searchStr = value;
                ChangeDisplay();
                RaisePropertyChanged("SearchStr");
            }
        }

        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods

        private void InitData()
        {
            List<LaborTemplateModel> list = LaborTemplateService.Instance.FindAll().ToList();
            if (DataManager.Instance.Login.WorkID.Equals("admin"))
            {
                foreach (var item in list)
                {
                    TemplateList.Add(item);
                    DisplayTemplateList.Add(item);
                }
            }
            else
            {
                foreach (var item in list)
                {
                    if (item.UserId.Equals(Constants.TEMPLATE_DEPARTMENT_ACCOUNTID) || DataManager.Instance.Login.Id.ToString().Equals(item.UserId))
                    {
                        TemplateList.Add(item);
                        DisplayTemplateList.Add(item);
                    }
                }
            }
            TempLateList2 = DisplayTemplateList.ToList();
            ChangeDisPlayList("gr");
        }

        private void ChangeDisplay()
        {
            DisplayTemplateList.Clear();
            foreach (var item in TemplateList)
            {
                string trueName = "";
                if (item.UserId.Equals(Constants.TEMPLATE_DEPARTMENT_ACCOUNTID))
                {
                    trueName = "麻醉科";
                }
                else
                {
                    trueName = DataManager.Instance.GetAccountNameByID(item.UserId);
                }
                string str = item.Name + trueName;
                if (str.IsContain(SearchStr))
                {
                    DisplayTemplateList.Add(item);
                }
            }
            TempLateList2 = DisplayTemplateList.ToList();

        }
        #endregion


        #region Public/ProtectedMethods
        public void DelAnesTemplate()
        {
            try
            {
                ObjectId templateId = SelectRecordTemplate.Id;
                int index = -1;
                for (int i = 0; i < TemplateList.Count; i++)
                {
                    if (SelectRecordTemplate.Id.Equals(TemplateList[i].Id))
                    {
                        index = i;
                        templateId = TemplateList[i].Id;
                        break;
                    }
                }
                if (index != -1)
                {
                    TemplateList.RemoveAt(index);
                }

                int index2 = -1;
                for (int i = 0; i < DisplayTemplateList.Count; i++)
                {
                    if (SelectRecordTemplate.Id.Equals(DisplayTemplateList[i].Id))
                    {
                        index2 = i;
                        break;
                    }
                }
                if (index2 != -1)
                {
                    DisplayTemplateList.RemoveAt(index2);
                }

                LaborTemplateService.Instance.DeleteById(templateId);
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), "del AnesTemplate failed!");
            }
        }

        public void ApplySelectTemplate(bool isDelCurData)
        {
            if (OperationDataPool.Instance.CurOperationData == null)
            {
                return;
            }
            if (SelectRecordTemplate == null)
            {
                MessageBoxCommon.ShowDialog("请先选择模板！", MessageBoxCommon.OK);
                return;
            }
            if (isDelCurData)
            {
                List<PatientLaborEventModel> exitList = PatientLaborEventService.Instance.GetPatientEvents8Time(DataManager.Instance.CurrentPatientLink.Id.ToString(), DataManager.Instance.CurrentPatientLink.Status.InLaborTime);
                foreach (var item in exitList)
                {
                    if (!("分娩镇痛开始").Equals(item.Name))
                    {
                        PatientLaborEventService.Instance.DeleteById(item.Id);
                    }
                }
            }
            UseTemplate();
            LaborAnalgesiaProcessViewModel.Instance.RefreshData();
        }

        private void UseTemplate()
        {
            DateTime startTime = DateTime.Now;
            List<PatientLaborEventModel> exitList = PatientLaborEventService.Instance.GetPatientEvents8Time(DataManager.Instance.CurrentPatientLink.Id.ToString(), DataManager.Instance.CurrentPatientLink.Status.InLaborTime);
            exitList.Sort((PatientLaborEventModel o1, PatientLaborEventModel o2) =>
            {
                if (o1.BeginTime == o2.BeginTime)
                {
                    if (o1.InputTime < o2.InputTime)
                    {
                        return -1;
                    }
                    else
                    {
                        return 1;
                    }
                }
                else if (o1.BeginTime < o2.BeginTime)
                {
                    return -1;
                }
                else if (o1.BeginTime > o2.BeginTime)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            });
            if (exitList != null && exitList.Count > 0)
            {
                PatientLaborEventModel ple = exitList.Last();
                startTime = ple.BeginTime;
            }
            else
            {
                startTime = PatientLink.Status.InLaborTime.AddMinutes(5);
            }
            List<PatientLaborEventModel> list = SelectRecordTemplate.TemplateList;
            if (list!=null && list.Count>0)
            {
                foreach (var item in list)
                {
                    startTime = startTime.AddMinutes(5);
                    PatientLaborEventModel ple = new PatientLaborEventModel();
                    ple.Name = item.Name;
                    ple.Code = item.Code;
                    ple.BeginTime = startTime;
                    ple.PatientID = PatientLink.Id.ToString();
                    PatientLaborEventService.Instance.AddUpdate(ple);
                }
            }
        }

    public void ChangeDisPlayList(string tag)
    {
        DisplayTemplateList.Clear();
        if (tag.Equals("ks"))
        {
            foreach (var item in TempLateList2)
            {
                if (Constants.TEMPLATE_DEPARTMENT_ACCOUNTID.Equals(item.UserId))
                {
                    DisplayTemplateList.Add(item);
                }
            }
        }
        if (tag.Equals("gr"))
        {
            foreach (var item in TempLateList2)
            {
                if (!Constants.TEMPLATE_DEPARTMENT_ACCOUNTID.Equals(item.UserId))
                {
                    DisplayTemplateList.Add(item);
                }
            }
        }
    }

    #endregion


    #region EventHandlers


    #endregion

}
}
